import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:personinfo/model/home/UserModel.dart';
import 'package:personinfo/states/appController.dart';

class Interceptor extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      // 在路由推送后检查 token，并在下一帧执行
      checkTokenAndNavigate(route.settings.name, route.navigator!.context);
    });
  }

  void checkTokenAndNavigate(String? routeName, BuildContext context) {
    // TODO: 在这里检查用户的登录状态，如果没有 token，则导航到登录页面
    AppController app = Get.put(AppController());
    bool hasToken = false;
    UserModel? userModel =  app.user;

    String token = userModel?.token ?? "";

    print("拦截器里面的token--：${token},当前路径：${routeName}");

    //白名单
    List urlPermission = ['/',"/setting","/splash"];

    if(token.isNotEmpty || urlPermission.contains(routeName)){
      hasToken = true;
    }else{
      hasToken = false;
    }


    if (!hasToken && routeName !=  '/login') {
      Navigator.of(context).pushReplacementNamed('/login');
    }

  }
}


